package com.example.project10;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import android.content.Context;

import java.util.List;

/**
 * 书籍列表ViewModel
 * 负责管理书籍列表界面的状态和数据
 */
public class BookViewModel extends ViewModel {
    
    private BookManager bookManager;
    
    // 书籍列表数据
    private MutableLiveData<List<Book>> bookListLiveData = new MutableLiveData<>();
    
    // 选中的书籍
    private MutableLiveData<Book> selectedBookLiveData = new MutableLiveData<>();
    
    // 操作状态消息
    private MutableLiveData<String> messageLiveData = new MutableLiveData<>();
    
    // 加载状态
    private MutableLiveData<Boolean> loadingLiveData = new MutableLiveData<>();
    
    // 输入字段
    private MutableLiveData<String> bookNumberLiveData = new MutableLiveData<>();
    private MutableLiveData<String> bookNameLiveData = new MutableLiveData<>();
    
    // 用户信息
    private String currentUserEmail;
    
    /**
     * 初始化ViewModel
     */
    public void initialize(Context context, String userEmail) {
        if (bookManager == null) {
            bookManager = new BookManager(context);
            currentUserEmail = userEmail;
            loadAllBooks();
        }
    }
    
    /**
     * 获取书籍列表LiveData
     */
    public LiveData<List<Book>> getBookList() {
        return bookListLiveData;
    }
    
    /**
     * 获取选中书籍LiveData
     */
    public LiveData<Book> getSelectedBook() {
        return selectedBookLiveData;
    }
    
    /**
     * 获取消息LiveData
     */
    public LiveData<String> getMessage() {
        return messageLiveData;
    }
    
    /**
     * 获取加载状态LiveData
     */
    public LiveData<Boolean> getLoading() {
        return loadingLiveData;
    }
    
    /**
     * 获取书号输入LiveData
     */
    public LiveData<String> getBookNumber() {
        return bookNumberLiveData;
    }
    
    /**
     * 获取书名输入LiveData
     */
    public LiveData<String> getBookName() {
        return bookNameLiveData;
    }
    
    /**
     * 设置书号输入
     */
    public void setBookNumber(String bookNumber) {
        bookNumberLiveData.setValue(bookNumber);
    }
    
    /**
     * 设置书名输入
     */
    public void setBookName(String bookName) {
        bookNameLiveData.setValue(bookName);
    }
    
    /**
     * 设置选中的书籍
     */
    public void setSelectedBook(Book book) {
        selectedBookLiveData.setValue(book);
        if (book != null) {
            bookNumberLiveData.setValue(book.getBookNumber());
            bookNameLiveData.setValue(book.getBookName());
        }
    }
    
    /**
     * 清除选中状态
     */
    public void clearSelection() {
        selectedBookLiveData.setValue(null);
    }
    
    /**
     * 清空输入框
     */
    public void clearInputs() {
        bookNumberLiveData.setValue("");
        bookNameLiveData.setValue("");
    }
    
    /**
     * 添加书籍
     */
    public void addBook() {
        if (bookManager == null) return;
        
        loadingLiveData.setValue(true);
        
        String bookNumber = bookNumberLiveData.getValue();
        String bookName = bookNameLiveData.getValue();
        
        BookManager.BookOperationResult result = bookManager.addBook(bookNumber, bookName, currentUserEmail);
        
        messageLiveData.setValue(result.getMessage());
        
        if (result.isSuccess()) {
            clearInputs();
            loadAllBooks();
        }
        
        loadingLiveData.setValue(false);
    }
    
    /**
     * 删除选中的书籍
     */
    public void deleteSelectedBook() {
        if (bookManager == null) return;
        
        Book selectedBook = selectedBookLiveData.getValue();
        if (selectedBook == null) {
            messageLiveData.setValue("请先选择要删除的书籍");
            return;
        }
        
        loadingLiveData.setValue(true);
        
        BookManager.BookOperationResult result = bookManager.deleteBook(selectedBook.getId(), currentUserEmail);
        
        messageLiveData.setValue(result.getMessage());
        
        if (result.isSuccess()) {
            clearSelection();
            loadAllBooks();
        }
        
        loadingLiveData.setValue(false);
    }
    
    /**
     * 更新选中的书籍
     */
    public void updateSelectedBook(String newBookNumber, String newBookName) {
        if (bookManager == null) return;
        
        Book selectedBook = selectedBookLiveData.getValue();
        if (selectedBook == null) {
            messageLiveData.setValue("请先选择要修改的书籍");
            return;
        }
        
        loadingLiveData.setValue(true);
        
        // 创建更新后的书籍对象
        Book updatedBook = new Book(
            selectedBook.getId(),
            newBookNumber,
            newBookName,
            selectedBook.getUserEmail()
        );
        
        BookManager.BookOperationResult result = bookManager.updateBook(updatedBook);
        
        messageLiveData.setValue(result.getMessage());
        
        if (result.isSuccess()) {
            clearSelection();
            loadAllBooks();
        }
        
        loadingLiveData.setValue(false);
    }
    
    /**
     * 搜索书籍
     */
    public void searchBooks() {
        if (bookManager == null) return;
        
        loadingLiveData.setValue(true);
        
        String bookNumber = bookNumberLiveData.getValue();
        String bookName = bookNameLiveData.getValue();
        
        BookManager.BookSearchResult result = bookManager.searchBooks(bookNumber, bookName, currentUserEmail);
        
        bookListLiveData.setValue(result.getBooks());
        messageLiveData.setValue(result.getMessage());
        
        loadingLiveData.setValue(false);
    }
    
    /**
     * 加载所有书籍
     */
    public void loadAllBooks() {
        if (bookManager == null) return;
        
        loadingLiveData.setValue(true);
        
        List<Book> books = bookManager.getUserBooks(currentUserEmail);
        
        if (books.isEmpty()) {
            bookManager.initializeUserData(currentUserEmail);
            books = bookManager.getUserBooks(currentUserEmail);
        }
        
        bookListLiveData.setValue(books);
        loadingLiveData.setValue(false);
    }
    
    /**
     * 获取当前书号输入值
     */
    public String getCurrentBookNumber() {
        String value = bookNumberLiveData.getValue();
        return value != null ? value : "";
    }
    
    /**
     * 获取当前书名输入值
     */
    public String getCurrentBookName() {
        String value = bookNameLiveData.getValue();
        return value != null ? value : "";
    }
    
    /**
     * 获取当前选中的书籍
     */
    public Book getCurrentSelectedBook() {
        return selectedBookLiveData.getValue();
    }
    
    @Override
    protected void onCleared() {
        super.onCleared();
        if (bookManager != null) {
            bookManager.close();
        }
    }
}